package com.example.hydrogengenerator.weight

import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.BaseInputConnection
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.view.inputmethod.InputMethodManager
import androidx.core.view.postDelayed

class NumberKeybordView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val inputManager: InputMethodManager by lazy(LazyThreadSafetyMode.NONE) { context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }

    var onNumberInputListener: ((text: String, backSpace: Boolean) -> Unit)? = null

    init {

        isFocusableInTouchMode = true
        isClickable = true
        isFocusable = true

        postDelayed(500) { showKeybord() }
        setOnClickListener {
            showKeybord()
        }
    }

    override fun onCheckIsTextEditor(): Boolean = true

    override fun onCreateInputConnection(outAttrs: EditorInfo?): InputConnection {
        outAttrs?.inputType = InputType.TYPE_CLASS_NUMBER
        return object : BaseInputConnection(this, false) {
            override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean {
                return super.commitText(text, newCursorPosition)
            }

            override fun sendKeyEvent(event: KeyEvent?): Boolean {
                event?.also {
                    when {
                        event.action == KeyEvent.ACTION_UP && event.keyCode == KeyEvent.KEYCODE_DEL -> {
                            onNumberInputListener?.invoke("", true)
                        }
                        event.action == KeyEvent.ACTION_UP && event.keyCode == KeyEvent.KEYCODE_ENTER -> {
                            clearFocus()
                            inputManager.hideSoftInputFromWindow(this@NumberKeybordView.windowToken, 0)
                        }
                        event.action == KeyEvent.ACTION_UP && event.keyCode == KeyEvent.KEYCODE_NUM -> {
                            onNumberInputListener?.invoke("", false)
                        }
                        event.action == KeyEvent.ACTION_UP && isNumCode(event.keyCode) -> {
                            onNumberInputListener?.invoke(numCode(event.keyCode), false)
                        }
                    }
                }

                return super.sendKeyEvent(event)
            }
        }

    }


    fun isNumCode(code: Int): Boolean {
        when (code) {
            KeyEvent.KEYCODE_0,
            KeyEvent.KEYCODE_1,
            KeyEvent.KEYCODE_2,
            KeyEvent.KEYCODE_3,
            KeyEvent.KEYCODE_4,
            KeyEvent.KEYCODE_5,
            KeyEvent.KEYCODE_6,
            KeyEvent.KEYCODE_7,
            KeyEvent.KEYCODE_8,
            KeyEvent.KEYCODE_9 -> return true
            else -> return false

        }
    }

    fun numCode(code: Int): String {
        return when (code) {
            KeyEvent.KEYCODE_0 -> "0"
            KeyEvent.KEYCODE_1 -> "1"
            KeyEvent.KEYCODE_2 -> "2"
            KeyEvent.KEYCODE_3 -> "3"
            KeyEvent.KEYCODE_4 -> "4"
            KeyEvent.KEYCODE_5 -> "5"
            KeyEvent.KEYCODE_6 -> "6"
            KeyEvent.KEYCODE_7 -> "7"
            KeyEvent.KEYCODE_8 -> "8"
            KeyEvent.KEYCODE_9 -> "9"
            else -> ""

        }
    }

    private fun showKeybord() {
        requestFocus()
        inputManager.showSoftInput(this, 0)
    }
}